package com.leet.competition;

import java.lang.reflect.Array;
import java.util.Arrays;

public class MinSessions {
    public int minSessions(int[] tasks, int sessionTime) {
        int n = tasks.length;
        int m  = 1 << n;
        int inf = 20;
        int[] dp = new int[m];
        Arrays.fill(dp,inf);
        for(int i = 1 ; i < m; i++){
            int state = i,index = 0;
            int spend = 0;
            // 对每个state（i）进行处理，和index联动
            while(state > 0){
                if((state & 1) == 1){
                    spend += tasks[index];
                }
                // 往右挪
                state >>= 1;
                index++;
            }
            if(spend <= sessionTime){
                dp[i] = 1;
            }
        }
        for (int i = 1; i < m; i++) {
            if(dp[i] == 1) continue;
            for (int j = 1; j <= i; j++) {
                if((j|i)==i){
                    dp[i] = Math.max(dp[i],dp[j]+dp[i^j]);
                }
            }
        }
        return dp[m-1];
    }
}
